package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Autonews;

public class GenericAutoNewsDAO extends AutoKeys implements
		org.jetnuke.dao.AutoNewsDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericAutoNewsDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.AutoNewsModel#retrieveByPK(int)
	 */
	public Autonews retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("AutoNewsModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		Autonews s = new Autonews();

		if (rs.next()) {
			fillAutoNewsFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillAutoNewsFromResultSet(Autonews s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillAutoNewsFromResultSet(Autonews, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setACOMM(rs.getInt("ACOMM"));
		s.setAID(rs.getInt("AID"));
		s.setALANGUAGE(rs.getString("ALANGUAGE"));
		s.setANID(rs.getInt("ANID"));
		s.setASSOCIATED(rs.getString("ASSOCIATED"));
		s.setBODYTEXT(rs.getString("BODYTEXT"));
		s.setCATID(rs.getInt("CATID"));
		s.setHOMETEXT(rs.getString("HOMETEXT"));
		s.setIHOME(rs.getInt("IHOME"));
		s.setINFORMANT(rs.getInt("INFORMANT"));
		s.setNOTES(rs.getString("NOTES"));
		s.setTIME(rs.getTimestamp("TIME1"));
		s.setTITLE(rs.getString("TITLE"));
		s.setTOPIC(rs.getInt("TOPIC"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillAutoNewsFromResultSet(Autonews, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.AutoNewsModel#addNew(org.jetnuke.entities.Autonews)
	 */
	public int addNew(Autonews news) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Autonews) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("AutoNewsModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("AutoNewsModel.lastGeneratedAutoNewsID"));
		initNewAuthoNews(news, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Autonews) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param news
	 * @param p
	 */
	private void initNewAuthoNews(Autonews n, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewAuthoNews(Autonews, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, n.getCATID());
		p.setInt(2, n.getAID());
		p.setString(3, n.getTITLE());
		p.setTimestamp(4, new Timestamp(n.getTIME().getTime()));
		p.setString(5, n.getHOMETEXT());
		p.setString(6, n.getBODYTEXT());
		p.setInt(7, n.getTOPIC());
		p.setInt(8, n.getINFORMANT());
		p.setString(9, n.getNOTES());
		p.setInt(10, n.getIHOME());
		p.setString(11, n.getALANGUAGE());
		p.setInt(12, n.getACOMM());
		p.setString(13, n.getASSOCIATED());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewAuthoNews(Autonews, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.AutoNewsModel#selectDateGreater(java.util.Date)
	 */
	public List selectDateGreater(Date date) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectDateGreater(Date) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("AutoNewsModel.selectDateGreater"));
		p.setTimestamp(1, new Timestamp(date.getTime()));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectDateGreater(Date) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Autonews u = new Autonews();
			fillAutoNewsFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.AutoNewsModel#deleteByID(int)
	 */
	public void deleteByID(int anid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("AutoNewsModel.deleteByID"));
		p.setInt(1, anid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - end"); //$NON-NLS-1$
		}
	}

}
